﻿/*
 * Summary:
 *   The FileReader class loads data from text file 
 *   and converts it to corresponding object for
 *   the sports store
 *   
 * History:
 *   Date           Author          Changes
 *   18/10/2010     YIWEI YU        Created
 *   31/10/2010     WEICAN WANG     Implemented the sports items reader 
 *                                  for basic file format
 *   01/11/2010     YIWEI YU        modified the sports items reader
 *                                  for reading enhanced file format
 */
//P487 Throwing Exceptions


using System;
using System.Collections.Generic;
using System.Collections;  
using System.Linq;
using System.Text;
using System.IO;    /* YIWEI YU: for file/stream manipulation */

namespace AcmeSportsStore
{
    class FileReader
    {
        private int i;  
        private string[] splits;
        private string readLine;
        StreamReader mSrSportsItems;
        StreamReader mSrCustomers;
        StreamReader mSrPurchasedItems;
        
        /*
         *  constructor of FileReader
         */
        public FileReader(StreamReader srSportsItems, StreamReader srCustomers, 
            StreamReader srPurchasedItems)
        {
            mSrSportsItems = srSportsItems;
            mSrCustomers = srCustomers;
            mSrPurchasedItems = srPurchasedItems;
        }

        public SportsStore ReadSportsItems()
        {
            SportsStore itemsIndexs = new SportsStore();
            this.readLine = mSrSportsItems.ReadLine();
            i = 0;
            while (this.readLine != null)
            {
                // Split out parts and create a sportItem object
                Console.WriteLine("The {0} line:", i);
                this.splits = readLine.Split(';');
       
                if (splits[0].Length == 0)
                {   /* invalid ID, ignore this line */
                    continue;
                }

                char idinit = splits[0][0];

                if (splits[0][0] == 'M')
                {   /* items without user gender specified */
                    itemsIndexs.AddItem(new SportsItem(
                        splits[0],
                        splits[1],
                        splits[2],
                        (int)(Convert.ToDecimal(splits[3]) * 100 + 0.5m)));
                }
                else if (splits[0][0] == 'G')
                {
                    SportsItemG.Gender gender;
                    if (splits[2] == "male")
                    {
                        gender = SportsItemG.Gender.Male;
                    }
                    else if (splits[2] == "female")
                    {
                        gender = SportsItemG.Gender.Female;
                    }
                    else
                    {
                        gender = SportsItemG.Gender.None;
                    }

                    itemsIndexs.AddItem(new SportsItemG(
                        splits[0],
                        splits[1],
                        splits[3],
                        (int)(Convert.ToDecimal(splits[4]) * 100 + 0.5m),
                        gender));
                }
                else
                {   /* invalid ID, just ignore */
                }

                this.readLine = mSrSportsItems.ReadLine();
               i++;
            }
            mSrSportsItems.Close();
            i = 0;
            return itemsIndexs;
        }

        /*
         * Load customer information from a stream
         */
        public Customers ReadCustomers()
        {
            Customers custIndexs = new Customers();
            this.readLine = mSrCustomers.ReadLine();
            i = 0;
            while (readLine != null)
            {
                // Split out parts and create a Customer object
                this.splits = readLine.Split(';');
                splits[3] = splits[3].Remove(4,1);  // remove the empty space among numbers
                if (splits[0].Length == 0)
                {   /* invalid ID, ignore this line */
                    continue;
                }
                if (splits[0].Length > 0)
                {
                        custIndexs.AddItem(new CustomersItem(
                        Convert.ToInt32(splits[0]),
                        splits[1],
                        splits[2],
                        Convert.ToInt32(splits[3])));
                }
                else
                {   /* invalid ID, just ignore */
                }

                this.readLine = mSrCustomers.ReadLine();
                i++;
            }
            mSrCustomers.Close();
            i = 0;
            return custIndexs;
        }

        public PurchasedStore ReadPurchasedItems()
        {  
            PurchasedStore PurchasedIndexs = new PurchasedStore();
            this.readLine = mSrCustomers.ReadLine();
            // for the unique Id for each purchased
            int uniqueId = 0;
            while (readLine != null)
            {
                // Split out parts and create a Customer object   
                this.splits = readLine.Split(';');
                if (splits[0].Length == 0)
                {   /* invalid ID, ignore this line */
                    continue;
                }
                if (splits[0].Length > 0)
                {
                    PurchasedIndexs.AddItem(new PurchasedItem(uniqueId,
                    splits[0],
                    splits[1],
                    splits[2],
                    splits[3],
                    splits[4]));
                }
                else
                {   /* invalid ID, just ignore */
                }
                this.readLine = mSrPurchasedItems.ReadLine();
                uniqueId++;
            }
            mSrPurchasedItems.Close();
            return PurchasedIndexs;
        }
    }
}
